package com.zbf.auth.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbf.common.entity.UserEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 作者: Mr.wangfa
 * 日期: 2020/8/26 20:28
 * 描述:
 */
@Mapper
public interface UserDao {

    /**
     * 依据用户名查询该用户
     * @param userName
     * @return
     */
    @Select("select * from base_user where loginName=#{userName}")
    public Map<String, Object> getUserByUserName(String userName);


    /**
     * 参数：
     * 返回值：
     * 描述: 获取当前user的所有的角色
     */
    @Select("select UPPER(br.role_code) roleCode from base_role br INNER JOIN base_user_role bur ON br.id=bur.roleId where bur.userId=#{userId}")
    public List<String> getUserRole(Long userId);

    /**
     * 依据手机号获取当前user
     * @param phoneNum 手机号
     * @return
     */
    @Select("select * from base_user where tel=#{phoneNum}")
    public Map<String, Object> getUserByUserTel(String phoneNum);

    /**
     * 依据邮箱获取当前user
     * @param email
     * @return
     */
    @Select("select * from base_user where email=#{email}")
    public Map<String, Object> getUserByUserEmail(String email);

    /**
    * @Author wangfa
    * @Description //TODO 注册用户时添加 数据
    * @Date 14:31 2020/9/20
    * @Param [userEntity]
    * @return boolean
    **/
    @Insert("insert into base_user (userName,loginName,passWord,tel,email,sex)values(#{userName}," +
            "#{loginName},#{passWord},#{tel},#{email},#{sex})")
    public boolean insertUser(UserEntity userEntity);

    @Select("select * from base_user where loginName=#{loginName}")
    public UserEntity selectUserById(String loginName);

    @Select(value = "insert into base_user_role (roleId,userId) values (#{roleId},#{id})")
    void insertUserRole(Long roleId, Long id);



    /**
     * 依据手机号修改用户密码
     * @param pwd
     * @param tel
     * @return
     */
    @Update("update base_user set passWord = #{pwd} where tel=#{tel}")
    public boolean updatePwd(String pwd,String tel);


    /**
    * @Author wangfa
    * @Description //TODO 查询用户列表
    * @Date 13:49 2020/9/18
    * @Param []
    * @return void
    **/

    @Select("select * from base_user")
    List<Map<String, Object>> getUserlist();

    /*@Select(value = "SELECT\n" +
            "\tbu.*,\n" +
            "\tGROUP_CONCAT( br.NAME ) rolename \n" +
            "FROM\n" +
            "\tbase_user bu\n" +
            "\tLEFT JOIN base_user_role bur ON bu.id = bur.userId\n" +
            "\tLEFT JOIN base_role br ON bur.roleId = br.id \n" +
            "WHERE\n" +
            "\tbu.userName LIKE concat( '%', #{userName}, '%' ) \n" +
            "\tAND bu.loginName LIKE concat( '%', #{loginName}, '%' ) \n" +
            "\tAND bu.tel LIKE concat( '%', #{tel}, '%' ) \n" +
            "GROUP BY\n" +
            "\tbu.id")*/

    @Select("<script> SELECT bu.*,GROUP_CONCAT(br.name)rolename,GROUP_CONCAT(br.id)bids  from base_user bu left join base_user_role bur  on(bu.id=bur.userId) left join base_role br on(bur.roleId=br.id) "+
            "<where>"+
            "<if test='userName != null and userName !=\"\"'>and bu.userName like CONCAT('%',#{userName},'%')</if> "+
            "<if test='loginName != null and loginName !=\"\"'>and bu.loginName like CONCAT('%',#{loginName},'%') </if>" +
            "<if test='tel != null and tel !=\"\"'>and bu.tel like CONCAT('%',#{tel},'%') </if>" +
            "</where>   "+
            "group by bu.id</script>"
    )
    IPage<UserEntity> selectPageVo(Page<?> page, String userName,String loginName,String tel);

    /**
    * @Author wangfa
    * @Description //TODO 用户列表数据的添加
    * @Date 14:31 2020/9/20
    * @Param [user]
    * @return boolean
    **/
    @Insert(value = "INSERT INTO base_user (userName, sex, img, loginName, `passWord`, tel, createTime, email,version ) VALUES (#{userName},#{sex},#{img},#{loginName},#{passWord},#{tel},#{createTime},#{email},0)")
    boolean addUser(UserEntity user);


    @Update("update base_user set version=#{status} where id=#{sid}")
    boolean setStatus(int status, Long sid);


    @Select(value = "insert into base_user_role(roleId,userId) values(#{id1},#{id})")
    void setRolefoUser(@Param("id") Long id, @Param("id1")Long id1);

    @Select(value = "delete from base_user_role where userId=#{id}")
    void deleteUserRole(Long id);

    @Update(value = "update base_user set  userName=#{userName},sex=#{sex},img=#{img},loginName=#{loginName},passWord=#{passWord},tel=#{tel},createTime=#{createTime},email=#{email},version=0 where id=#{id}")
    boolean updateUser(UserEntity user);

    @Select(value = "select id from base_role where name=#{id}")
    Long selectRoleid(String id);
}